package com.holdtime.jtt809.taxi.module.web.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.holdtime.jtt809.taxi.module.entity.CarRegisterDO;
import com.holdtime.jtt809.taxi.module.model.vo.CarRegisterPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.CarRegisterRespVO;
import com.holdtime.jtt809.taxi.module.model.vo.CarRegisterSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.CarRegisterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 车辆注册信息")
@RestController
@RequestMapping("/taxi/car-register")
@Validated
public class CarRegisterController {

    @Resource
    private CarRegisterService carRegisterService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆注册信息")
    @PreAuthorize("@ss.hasPermission('taxi:car-register:create')")
    public CommonResult<String> createCarRegister(@Valid @RequestBody CarRegisterSaveReqVO createReqVO) {
        return success(carRegisterService.createCarRegister(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆注册信息")
    @PreAuthorize("@ss.hasPermission('taxi:car-register:update')")
    public CommonResult<Boolean> updateCarRegister(@Valid @RequestBody CarRegisterSaveReqVO updateReqVO) {
        carRegisterService.updateCarRegister(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆注册信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('taxi:car-register:delete')")
    public CommonResult<Boolean> deleteCarRegister(@RequestParam("id") String id) {
        carRegisterService.deleteCarRegister(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆注册信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('taxi:car-register:query')")
    public CommonResult<CarRegisterRespVO> getCarRegister(@RequestParam("id") String id) {
        CarRegisterDO carRegister = carRegisterService.getCarRegister(id);
        return success(BeanUtils.toBean(carRegister, CarRegisterRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆注册信息分页")
    @PreAuthorize("@ss.hasPermission('taxi:car-register:query')")
    public CommonResult<PageResult<CarRegisterRespVO>> getCarRegisterPage(@Valid CarRegisterPageReqVO pageReqVO) {
        PageResult<CarRegisterDO> pageResult = carRegisterService.getCarRegisterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CarRegisterRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆注册信息 Excel")
    @PreAuthorize("@ss.hasPermission('taxi:car-register:export')")
    public void exportCarRegisterExcel(@Valid CarRegisterPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CarRegisterDO> list = carRegisterService.getCarRegisterPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆注册信息.xls", "数据", CarRegisterRespVO.class,
                        BeanUtils.toBean(list, CarRegisterRespVO.class));
    }

}